var express = require('express');
var router = express.Router();
var path = require('path')
var formidable = require('formidable');
fs = require('fs');


router.post('/', (req, res) => {
  var form = new formidable.IncomingForm()
  form.encoding = 'utf-8' // 编码
  form.keepExtensions = true // 保留扩展名
  form.uploadDir = path.join(__dirname, '../public/images/')
  form.parse(req, (err, fields, files) => {
    console.log('files', files) //上传文件用files.<name>访问
    if (err) return next(err)
    console.log('files', files) //上传文件用files.<name>访问
    fs.writeFileSync(`public/images/${files.file.name}`, fs.readFileSync(files.file.path));
    res.send({
      code: 1,
      imgUrl: `http://42.192.94.252:3000/images/${files.file.name}`
      //
    })
  })

});

module.exports = router